
package com.platform.modules.bk.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.utils.Query;
import com.platform.common.utils.StringUtils;
import com.platform.modules.bk.dao.BkCustomerDao;
import com.platform.modules.bk.entity.BkCustomerEntity;
import com.platform.modules.bk.service.BkCustomerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 客户号对应数据Service实现类
 *
 * @author
 * @date 2022-03-14 16:04:07
 */
@Service("bkCustomerService")
public class BkCustomerServiceImpl extends ServiceImpl<BkCustomerDao, BkCustomerEntity> implements BkCustomerService {

    @Override
    public List<BkCustomerEntity> queryAll(Map<String, Object> params) {
        return baseMapper.queryAll(params);
    }

    @Override
    public Page queryPage(Map<String, Object> params) {
        //排序
        params.put("sidx", "T.CUSTOMER_NO");
        params.put("asc", false);
        Page<BkCustomerEntity> page = new Query<BkCustomerEntity>(params).getPage();
        return page.setRecords(baseMapper.selectBkCustomerPage(page, params));
    }

    @Override
    public boolean add(BkCustomerEntity bkCustomer) {
        return this.save(bkCustomer);
    }

    @Override
    public boolean update(BkCustomerEntity bkCustomer) {
        return this.updateById(bkCustomer);
    }

    @Override
    public boolean updatePoints(BigDecimal actualPrice, Integer type, String customerNo) {
        BkCustomerEntity customerEntity = this.getById(customerNo);
        if (customerEntity == null || StringUtils.isBlank(customerEntity.getCustomerNo())) {
            return false;
        }
        return this.update(new LambdaUpdateWrapper<BkCustomerEntity>()
                .set(type == 1, BkCustomerEntity::getAvailablePoints, customerEntity.getAvailablePoints().subtract(actualPrice))
                .set(type == 0, BkCustomerEntity::getAvailablePoints, customerEntity.getAvailablePoints().add(actualPrice))
                .eq(BkCustomerEntity::getCustomerNo, customerNo));
    }

    @Override
    public boolean delete(String customerNo) {
        return this.removeById(customerNo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(String[] customerNos) {
        return this.removeByIds(Arrays.asList(customerNos));
    }
}
